import { Injectable } from '@angular/core';

@Injectable()
export class StorageService {
    /**
     * localStorage
     * 保存session
     */
    // 保存
    setItem(key: string, value: any): void {
        localStorage.setItem(key, JSON.stringify(value));
    }
    // 获取
    getItem(key: string): any {
        let value = localStorage.getItem(key); 
        return value ? JSON.parse(value) : value;
    }
    // 删除
    removeItem(key: string): void {
        localStorage.removeItem(key);
    }

    /**
     * sessionStorage
     * 保存一些临时信息
     */
    // 保存
    setSessionItem(key: string, value: any): void {
        sessionStorage.setItem(key, JSON.stringify(value));
    }
    // 获取
    getSessionItem(key: string): any {
        let value = sessionStorage.getItem(key); 
        return value ? JSON.parse(value) : value;
    }
    // 删除
    removeSessionItem(key: string): void {
        sessionStorage.removeItem(key);
    }
}